import { fetchAccountLogin } from '@/services/user'
import { message } from 'antd'

export default {
  namespace: 'user',

  state: {
    status: undefined,
    res: {}
  },

  effects: {
    *login({ payload }, { call, put }) {
      console.log('user login')
      const res = yield call(fetchAccountLogin, payload)
      // yield put({
      //   type: 'changeLoginStatus',
      //   payload: {
      //     res
      //   }
      // })
      if (res.errcode === 0) {
        localStorage.setItem('token', res.data.token)
        yield put({
          type: 'changeLoginStatus',
          payload: {
            status: true
          }
        })
      } else {
        message.error(res.errmsg)
      }
    },

    *logout(_, { call, put }) {
      console.log('user logout')
      localStorage.clear('token')
      yield put({
        type: 'changeLoginStatus',
        payload: {
          status: false
        }
      })
    }
  },

  reducers: {
    changeLoginStatus(state, { payload }) {
      return {
        ...state,
        status: payload.status,
        // res: payload.res || {}
      }
    }
  }
}